﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http.Filters;
using DSAServer.Models;

namespace DSAServer.Utils
{
    public class MyExceptionFilterAttribute : ExceptionFilterAttribute
    {
        public override void OnException(HttpActionExecutedContext context)
        {
            context.Response=new HttpResponseMessage(HttpStatusCode.OK);

            if (context.Exception is ArgumentException)
            {
                context.Response.Content = new StringContent(JsonHelper.SerializeObject(new InvokeResult() { msg = context.Exception.Message }));
            }
            else
            {
                context.Response.Content = new StringContent(JsonHelper.SerializeObject(new InvokeResult() { msg = "An error occurred while executing" }));
            }
        }
    } 
}